<?php
/**
 * 客户订单发货明细
 * (model)
 */

namespace App\Models\CustomerDelivery;


use App\Models\Basics\SysPlatAccount;
use App\Models\OverSeasBa\Warehouse;
use App\Models\Product\ProductClassify;
use App\Models\Product\ProductSku;
use Illuminate\Database\Eloquent\Model;

class CustomerDeliveryDetail extends Model{

    protected $table = 'customer_delivery_detail';

    protected $guarded  = [];

    /**
     * 状态
     * @var string[]
     */
    const STATUS_READY_NO      = 1; //待备货
    const STATUS_READY_YES     = 2; //已备货
    const STATUS_DELIVERY_NO   = 3; //待发货
    const STATUS_DELIVERY_PART = 4; //部分发货
    const STATUS_DELIVERY_YES  = 5; //已发货
    const STATUS_DELIVERY_ERROR  = 6; //异常
    const STATUS_DELIVERY_CANCEL  = 7; //取消
    public static $_status = [
        self::STATUS_READY_NO       => '待备货',
        self::STATUS_READY_YES      => '已备货',
        self::STATUS_DELIVERY_NO    => '待发货',
        self::STATUS_DELIVERY_PART  => '部分发货',
        self::STATUS_DELIVERY_YES   => '已发货',
        self::STATUS_DELIVERY_ERROR   => '异常',
        self::STATUS_DELIVERY_CANCEL   => '已取消',
    ];

    //FBA状态查询
    public static $_fbaStatusOption = [
        self::STATUS_DELIVERY_NO    => '待发货',
        self::STATUS_DELIVERY_PART  => '部分发货',
        self::STATUS_DELIVERY_YES   => '已发货',
        self::STATUS_DELIVERY_ERROR   => '异常',
        self::STATUS_DELIVERY_CANCEL   => '已取消',
    ];

    //国内状态查询
    public static $_cnStatusOption = [
        self::STATUS_READY_NO       => '待备货',
        self::STATUS_READY_YES      => '已备货',
        self::STATUS_DELIVERY_NO    => '待发货',
        self::STATUS_DELIVERY_PART    => '部分发货',
        self::STATUS_DELIVERY_YES   => '已发货',
        self::STATUS_DELIVERY_ERROR   => '异常',
        self::STATUS_DELIVERY_CANCEL   => '已取消',
    ];

    /**
     * 错误状态
     * @var string[]
     */
    const STATUS_NORMAL = 0; //正常
    const STATUS_ERROR  = 1; //ERROR
    public static $_errorStatus = [
        self::STATUS_NORMAL => '正常',
        self::STATUS_ERROR  => 'ERROR'
    ];

    /**
     * 平台(追踪号)物流同步状态
     * @var string[]
     */
    const SYNC_STATUS_NO  = 0; //未同步
    const SYNC_STATUS_YES = 1; //已同步
    public static $_syncStatus = [
        self::SYNC_STATUS_NO  => '未同步',
        self::SYNC_STATUS_YES => '已同步',
    ];

    /*public static $_logisticsType = [
        0 => '无',
        1 => '海运',
        2 => '空运',
        3 => '铁运',
    ];

    public static $_deliverySLA = [
        1 => 'Standard',
        2 => 'Expedited',
        3 => 'Priority',
    ];*/

    public static $_rakutenCompany = [
        'ヤマト運輸' => '大和运输',
        '佐川急便'   => '佐川急便',
        '日本郵便'   => '日本邮政',
        'その他'     => '其他'
    ];

    /**
     * 物流费用
     */
    const LOGISTICS_FEE_NO  = 0; //未更新
    const LOGISTICS_FEE_YES = 1; //已更新
    public static $_logistics_fee = [
        self::LOGISTICS_FEE_NO => '未更新',
        self::LOGISTICS_FEE_YES => '已更新'
    ];

    public static $_logisticsStatus = [
        1 => '待更新',
        2 => '已更新',
    ];

    /**
     * 关联发货单
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function delivery(){
        return $this->belongsTo(CustomerDelivery::class, 'delivery_no', 'delivery_no');
    }

    /**
     * 关联sku
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function sku(){
        return $this->belongsTo(ProductSku::class, 'psku_id');
    }

    public function skuClassify(){
        return $this->hasOneThrough(
            ProductClassify::class,
            ProductSku::class,
            'id',//中间表
            'id',//目标表
            'psku_id',//当前模型
            'classify_id'//中间表
        );
    }

    /**
     * 关联地址
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function receiver(){
        return $this->belongsTo(CustomerDeliveryReceiver::class, 'delivery_no', 'delivery_no');
    }

    /**
     * 关联账号
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function account(){
        return $this->belongsTo(SysPlatAccount::class, 'account_id');
    }

    /**
     * 关联订单明细
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function order_detail(){
        return $this->belongsTo(CustomerOrderDetails::class, 'order_detail_id');
    }

    /**
     * 关联订单明细
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function order(){
        return $this->belongsTo(CustomerOrder::class, 'order_no', 'order_no');
    }

    public function warehouse(){
        return $this->belongsTo(Warehouse::class, 'warehouse_id');
    }

    /**
     * FBA发货状态：依据子表状态同步主表状态
     * 3 待发
     * 5 已发(ALL)
     * 6 异常
     * 7 取消
     * 4 部分发货
     * @param $status
     * @return int
     */
    public static function extractStatus($status){
        if(max($status) == 3)return 3;
        if(min($status) == 6) return 6;
        if(min($status) == 7) return 7;

        if(min($status) == 5 && max($status) == 5) return 5;

        //$status = [3,5,5];
        //if(max($status) == 5){
        //    $arr = [];
        //    foreach ($status as $key => $val){
        //        if ($val != 5) continue;
        //        $arr[] = $val;
        //    }
        //    if (count($arr) != count($status)) {
        //        return 4;
        //    }
        //
        //    return 5;
        //}

        return 4;
    }

    public static function getDeliveryDetails($deliveryNo, $orderNo){
       return self::query()
            ->where('delivery_no',$deliveryNo)
            ->where('order_no',$orderNo)
            ->get();
    }

}
